//
//  shopPickerViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "ShopPickerViewController.h"


@implementation ShopPickerViewController
@synthesize shopPickerDataSource = _shopPickerDataSource;
@synthesize shopPickerView = _shopPickerView;
@synthesize showLabel = _showLabel;
@synthesize applyBtn = _applyBtn;
@synthesize cancelBtn = _cancelBtn;

/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.shopPickerDataSource = [[ShopPickerDataSource alloc] init];
    }
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [_applyBtn release];
    [_cancelBtn release];
    [_showLabel release];
    [_shopPickerDataSource release];
    [_shopPickerView release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"거래처 선택";
    self.shopPickerView.dataSource = _shopPickerDataSource;
    if([_shopPickerDataSource.shopPickerArray count]>0){
        [self.showLabel setText:[[_shopPickerDataSource.shopPickerArray objectAtIndex:[_shopPickerView selectedRowInComponent:0]] objectForKey:@"title"]];
    }else{
        [self.showLabel setText:@""];
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    self.applyBtn=nil;
    self.cancelBtn=nil;
    self.showLabel=nil;
    self.shopPickerDataSource = nil;
    self.shopPickerView = nil;
    [super viewDidUnload];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_shopPickerDataSource reloadData];
    [_shopPickerView reloadAllComponents];
    if([_shopPickerDataSource.shopPickerArray count]<=[_shopPickerView selectedRowInComponent:0]){
        [_shopPickerView selectRow:0 inComponent:0 animated:YES];
        [_resultDelegate applyResultSet:self resultDic:nil];
        [self.showLabel setText:@""];
    }else{
        [self.showLabel setText:[[_shopPickerDataSource.shopPickerArray objectAtIndex:[_shopPickerView selectedRowInComponent:0]] objectForKey:@"title"]];    
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark -
#pragma mark UIPickerViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//    NSLog(@"didSelectRow");
//	if (pickerView == _shopPickerView){
//        _shopPickerDataSource.shopName = [[_shopPickerDataSource.shopPickerArray objectAtIndex:row] objectForKey:@"title"];                
//        _shopPickerDataSource.shopId = [[_shopPickerDataSource.shopPickerArray objectAtIndex:row] objectForKey:@"shop_id"];
//        [self.showLabel setText:_shopPickerDataSource.shopName];
//	}
    [self.showLabel setText:[[_shopPickerDataSource.shopPickerArray objectAtIndex:row] objectForKey:@"title"]];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //    NSLog(@"titleForRow");
	NSString *returnStr = @"";
    returnStr = [[_shopPickerDataSource.shopPickerArray objectAtIndex:row] objectForKey:@"title"];            
    return returnStr;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    //    NSLog(@"rowHeightForComponent");
	return 40.0;
}
#pragma mark UIAlertViewDelegate mothods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    
    if(buttonIndex==0){
        [self openShopManage:nil];
    }
}
#pragma mark -
#pragma mark UIButtonDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)applyAct:(id)sender{
    NSLog(@"applyAct");
    if([_shopPickerDataSource.shopPickerArray count]==0){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"웁스" message:@"목록이 없습니다.\n목록을 추가 하시겠습니까?." delegate:self cancelButtonTitle:nil otherButtonTitles:@"확인", @"아니오", nil];
        [alert show];	
        [alert release];                
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        [dic setObject:[[_shopPickerDataSource.shopPickerArray objectAtIndex:[_shopPickerView selectedRowInComponent:0]] objectForKey:@"shop_id"] forKey:@"shopId"];
        [dic setObject:[[_shopPickerDataSource.shopPickerArray objectAtIndex:[_shopPickerView selectedRowInComponent:0]] objectForKey:@"title"] forKey:@"shopName"];
        
        [_resultDelegate applyResultSet:self resultDic:dic];

        [[self navigationController] popViewControllerAnimated:YES];
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)cancelAct:(id)sender{
    NSLog(@"cancelAct");
    [[self navigationController] popViewControllerAnimated:YES];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(IBAction)openShopManage:(id)sender{
    ShopSettingViewController *viewController = [[[ShopSettingViewController alloc] initWithNibName:@"ShopSettingView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];                    
}
@end
